'''

Case Type： 控制文件
Case Name： 控制文件删除
Create At:  2020/07/01
Owner    :  @wan005
Descption:  1.控制文件删除，数据库运行时checkpoint，启动数据库、停止数据库 

'''
import os
import sys
import unittest
import time
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common


class DelCtrl(unittest.TestCase):
    sh = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case121.py start------------')
        self.dbUserNode = Node(node='dbuser')
        self.PG_CONTROL_PATH = macro.PG_CONTROL_PATH
        self.com = Common()
        self.Constant = Constant()

    def test_del_ctrl(self):
        # 删除控制文件及备份文件
        cmd = f'mv {self.PG_CONTROL_PATH}* {macro.DB_BACKUP_PATH}'
        self.log.info(cmd)
        msg = self.dbUserNode.sh(cmd).result()
        self.log.info(msg)

        # 数据库运行时checkpoint
        cmd = "checkpoint;"
        self.log.info(cmd)
        msg = self.sh.executDbSql(cmd)
        self.assertIn(self.Constant.FAILED_CHECKPOINT, msg)

        # 停止数据库
        is_stop = self.sh.stopDbCluster()
        self.assertTrue(is_stop)

        time.sleep(2)

        # 启动数据库
        is_started = self.sh.startDbCluster()
        self.assertFalse(is_started)

    def tearDown(self):
        cmd = f'mv {macro.DB_BACKUP_PATH}/pg_control* {os.path.dirname(self.PG_CONTROL_PATH)}'
        self.log.info(cmd)
        msg = self.dbUserNode.sh(cmd).result()
        self.log.info(msg)
